<?php
/**
 * Created by PhpStorm.
 * User: ewen
 * Date: 2020-01-03
 * Time: 17:21
 */

namespace app\common\model;



class XkAdminAuth extends Common
{
    /**
     * 获取权限列表
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public static function getAuthList()
    {
        if (cache(Common::CACHE_ADMIN_AUTH)) {
            $list = cache(Common::CACHE_ADMIN_AUTH);
        } else {
            $list = self::field('id,authName,authPID,authType,isShow')->where([
                'isDel' => 0
            ])->order('id asc')->select();
            $list = self::getTree($list);
            cache(Common::CACHE_ADMIN_AUTH, $list);
        }
        return $list;
    }

    /**
     * 获得无限树形结构
     * @param $list
     * @param int $parent_id
     * @return array
     */
    static function getTree($list, $parent_id = 0)
    {
        $temp = array();
        foreach ($list as $k => $v) {
            if ($v['authPID'] == $parent_id) {
                $temp[$k] = $v;
                $temp[$k]['sons'] = array_values(self::getTree($list, $v['id']));
            }
        }
        return array_values($temp);
    }
}